package iceCream.core.base
{
	import flash.display.Stage3D;
	import flash.display3D.Context3D;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.VertexBuffer3D;
	
	/**
	 * Project : IceCream
	 * SubGeometry
	 * @author 赵俊 <zhaojun@crop.the9.com>  
	 * $Id:$
	 * @version 1.0
	 */
	public class IC_SubGeometry
	{
		//原始数据
		protected var _vertices:Vector.<Number>;
		
		protected var _indices:Vector.<uint>;
		protected var _uvs:Vector.<Number>;
		
		//加工后的数据
		/**
		 *顶点buffer 
		 */
		protected var _vertexBuffer:VertexBuffer3D;
		
		/**
		 * uv buffer。 
		 */
		protected var _uvBuffer:VertexBuffer3D;
		
		/**
		 * 顶点顺序列表。 
		 */
		protected var _indicesBuffer:IndexBuffer3D;
		
		//脏数据标识
		private var _verticesDirty:Boolean = false;
		private var _uvDirty:Boolean = false;
		private var _indiceDirty:Boolean = false;
		
		public function IC_SubGeometry()
		{
		}

		/**
		 * 顶点坐标数据，每个顶点3个
		 */
		public function get vertices():Vector.<Number>
		{
			return _vertices;
		}

		/**
		 * @private
		 */
		public function set vertices(value:Vector.<Number>):void
		{
			_vertices = value;
			_verticesDirty = true;
		}

		/**
		 * 顶点顺序列表 
		 */
		public function get indices():Vector.<uint>
		{
			return _indices;
		}

		/**
		 * @private
		 */
		public function set indices(value:Vector.<uint>):void
		{
			_indices = value;
			_verticesDirty = true;
		}

		/**
		 * uv信息 ，每个顶点2个。
		 */
		public function get uvs():Vector.<Number>
		{
			return _uvs;
		}

		/**
		 * @private
		 */
		public function set uvs(value:Vector.<Number>):void
		{
			_uvs = value;
			_uvDirty = true;
		}

		public function getVertexBuffer(stage3D:Stage3D):VertexBuffer3D
		{
			if(!_verticesDirty)
			{
				return _vertexBuffer;
			}else
			{
				if(_vertexBuffer != null)
				{
					_vertexBuffer.dispose();
				}
				var numberVertices:int = _vertices.length/3;
				_vertexBuffer = stage3D.context3D.createVertexBuffer(numberVertices,3);
				_vertexBuffer.uploadFromVector(_vertices,0,numberVertices);
				_verticesDirty = false;
			}
			return _vertexBuffer;
		}
		
		public function getUVBuffer(stage3D:Stage3D):VertexBuffer3D
		{
			if(!_uvDirty)
			{
				return _uvBuffer;
			}else
			{	
				if(_uvBuffer != null)
				{
					_uvBuffer.dispose();
				}
				var numberVertices:int = _vertices.length/3;
				_vertexBuffer = stage3D.context3D.createVertexBuffer(numberVertices,3);
				_vertexBuffer.uploadFromVector(_vertices,0,numberVertices);
				_uvDirty = false;
			}
			return _vertexBuffer;
			
		}
		
		public function getIndices(stage3D:Stage3D):IndexBuffer3D
		{
			if(!_indiceDirty)
			{
				return _indicesBuffer;
			}else
			{
				if(_indicesBuffer != null)
				{
					_indicesBuffer.dispose();
				}
				var indicesNum:int = _indices.length;
				_indicesBuffer = stage3D.context3D.createIndexBuffer(indicesNum);
				_indicesBuffer.uploadFromVector(_indices,0,indicesNum);
				_indiceDirty = false;
			}
			return _indicesBuffer;
		}

	}
} 
